package com.fly.caihong.frag;

import android.os.Bundle;
import android.os.Looper;
import android.os.MessageQueue;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.util.Pair;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.fly.caihong.R;
import com.fly.caihong.activity.VideoDetailActivity;
import com.fly.caihong.adapter.VideoAdapter;
import com.fly.caihong.base.BasePageListFragment;
import com.fly.caihong.model.VideoItem;
import com.fly.caihong.model.VideoListItem;
import com.fly.caihong.utils.DataHandler;
import com.fly.caihong.utils.ItemSpacesDecoration;
import com.fly.caihong.utils.SizeUtils;
import com.fly.caihong.utils.StatusBarUtils;
import com.fly.caihong.utils.ToastUtils;
import com.fly.caihong.view.PullRefreshRecyclerView;

public class VideoFragment extends BasePageListFragment {

    VideoListItem videoListItem;
    VideoAdapter mAdapter;
    View edtSearch;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        videoListItem = DataHandler.convertJsonData("video", VideoListItem.class);
        super.onCreate(savedInstanceState);
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        if (hidden) {
            StatusBarUtils.setFullScreenLightMode(getActivity());
        } else {
            Looper.myQueue().addIdleHandler(new MessageQueue.IdleHandler() {
                @Override
                public boolean queueIdle() {
                    StatusBarUtils.setDarkMode(getActivity());
                    return false;
                }
            });
        }
    }

    @Override
    protected Pair<PullRefreshRecyclerView, BaseQuickAdapter> initPullRefreshView(View view) {
        mAdapter = new VideoAdapter(videoListItem.getVideoList());
        return new Pair(view.findViewById(R.id.pull_refresh_layout), mAdapter);
    }

    @Override
    protected void loadPageData(int pageNum) {
        mPullRefreshView.loadMoreEnd();
    }

    @Override
    protected int getLayout() {
        return R.layout.fragment_video;
    }

    @Override
    protected void initView(View view) {
        super.initView(view);
        if (videoListItem == null) {
            ToastUtils.showLong("视频列表数据为空");
            return;
        }
        int margin = SizeUtils.dp2px(2);
        edtSearch = view.findViewById(R.id.edt_search);
        mPullRefreshView.getmRecyclerView().addItemDecoration(new ItemSpacesDecoration(margin, margin, margin, margin));
        mPullRefreshView.setAdapter(mAdapter, new GridLayoutManager(getActivity(), 2));
    }

    @Override
    public void onRefresh() {
        dismissRefreshStatus();
    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        VideoDetailActivity.launchActivity(getActivity(), ((VideoItem) adapter.getItem(position)));
    }

    @Override
    protected void initListener() {
        super.initListener();
        mAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                if (view.getId() == R.id.rl_root || view.getId() == R.id.iv_pause) {
                    VideoDetailActivity.launchActivity(getActivity(), ((VideoItem) adapter.getItem(position)));
                }
            }
        });
    }

    @Override
    public void initData() {
        super.initData();
        dismissRefreshStatus();
    }

}
